package com.koudai.reader.conponment;

import java.util.UUID;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.iapppay.mpay.ifmgr.IPayResultCallback;
import com.iapppay.mpay.ifmgr.SDKApi;
import com.iapppay.mpay.tools.PayRequest;
import com.iapppay.pay.mobile.iapppaysecservice.utils.IAppPaySDKConfig;
import com.koudai.reader.util.HttpUtils;
import com.koudai.reader.vo.EndUser;

public class PayUtil {

    /**
         * 
         */
    public static void pay(final String operation, final int money, final Activity context) {
        PayRequest payRequest = new PayRequest();
        payRequest.addParam("quantity ", 1);
        payRequest.addParam("appid", IAppPaySDKConfig.APP_ID);
        payRequest.addParam("waresid", IAppPaySDKConfig.WARES_ID_1);
        payRequest.addParam("exorderno", UUID.randomUUID().toString());
        payRequest.addParam("price", money);
        payRequest.addParam("cpprivateinfo", EndUser.getInstance().getUid());
        String paramUrl = payRequest.genSignedUrlParamString(IAppPaySDKConfig.APP_KEY);

        SDKApi.startPay(context, paramUrl, new IPayResultCallback() {
            @Override
            public void onPayResult(int resultCode, String signValue, String resultInfo) {
                if (SDKApi.PAY_SUCCESS == resultCode) {
                    Log.e("xx", "signValue = " + signValue);
                    if (null == signValue) {
                        // 没有签名值，默认采用finish()，请根据需要修改
                        Log.e("xx", "signValue is null ");
                        Toast.makeText(context, "没有签名值", Toast.LENGTH_SHORT).show();
                        // //finish();
                    }
                    final boolean flag = PayRequest.isLegalSign(signValue, IAppPaySDKConfig.APP_KEY);

                    new Thread(new Runnable() {

                        @Override
                        public void run() {

                            if (HttpUtils.goldOperation(operation) && flag) {

                                if (operation.startsWith("recharge")) {
                                    // 如果是充值，更新money
                                    EndUser.getInstance().updateGold(money);

                                } else if (operation.startsWith("vip")) {
                                    // 如果是VIP，只改变vip的状态
                                    EndUser.getInstance().setVip(true);
                                }

                                try {
                                    HttpUtils.requestUserinfo(context);
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                                context.runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        Toast.makeText(context, "支付成功,谢谢您对书海小说的支持 ^_^", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                        }
                    }).start();

                } else if (SDKApi.PAY_CANCEL == resultCode) {
                    Toast.makeText(context, "取消支付", Toast.LENGTH_SHORT).show();
                    // 取消支付处理，默认采用finish()，请根据需要修改
                    Log.e("fang", "return cancel");
                } else {
                    Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show();
                    // 计费失败处理，默认采用finish()，请根据需要修改
                    Log.e("fang", "return Error");
                }

            }
        });
    }

    
    public static void pay(final int money, final Activity context) {
        PayRequest payRequest = new PayRequest();
        payRequest.addParam("quantity ", 1);
        payRequest.addParam("appid", IAppPaySDKConfig.APP_ID);
        payRequest.addParam("waresid", IAppPaySDKConfig.WARES_ID_1);
        payRequest.addParam("exorderno", UUID.randomUUID().toString());
        payRequest.addParam("price", money);
        payRequest.addParam("cpprivateinfo", EndUser.getInstance().getUid());
        String paramUrl = payRequest.genSignedUrlParamString(IAppPaySDKConfig.APP_KEY);

        SDKApi.startPay(context, paramUrl, new IPayResultCallback() {
            @Override
            public void onPayResult(int resultCode, String signValue, String resultInfo) {
                if (SDKApi.PAY_SUCCESS == resultCode) {
                    Log.e("xx", "signValue = " + signValue);
                    if (null == signValue) {
                        // 没有签名值，默认采用finish()，请根据需要修改
                        Log.e("xx", "signValue is null ");
                        Toast.makeText(context, "没有签名值", Toast.LENGTH_SHORT).show();
                        // //finish();
                    }
                    final boolean flag = PayRequest.isLegalSign(signValue, IAppPaySDKConfig.APP_KEY);

                    new Thread(new Runnable() {

                        @Override
                        public void run() {

                            if (HttpUtils.addGold4user(money) && flag) {

                                try {
                                    HttpUtils.requestUserinfo(context);
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                                context.runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        Toast.makeText(context, "支付成功,谢谢您对书海小说的支持 ^_^", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                        }
                    }).start();

                } else if (SDKApi.PAY_CANCEL == resultCode) {
                    Toast.makeText(context, "取消支付", Toast.LENGTH_SHORT).show();
                    // 取消支付处理，默认采用finish()，请根据需要修改
                    Log.e("fang", "return cancel");
                } else {
                    Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show();
                    // 计费失败处理，默认采用finish()，请根据需要修改
                    Log.e("fang", "return Error");
                }

            }
        });
    }
}
